package com.personal.kousuan.generator;

import java.util.Random;

import com.personal.kousuan.domain.Question;
import com.personal.kousuan.factory.RandomGeneratorFactory;
import com.personal.kousuan.random.RandomGenerator;
import com.personal.kousuan.util.RandomUtil;

public abstract class MinusKouSuanGenerator implements KouSuanGenerator {

	

	@Override
	public Question generateKouSuanTi() {
		
		int one = 0;
		int i = 0;
		int j = 9;
		
		while(j == 9){
			//随机产生第一个数
			one = RandomUtil.generateRandom(getMin(), getMax());
			
			//第一个数个位
			i = one % 10;
			//第一个数十位
			j = one / 10;
			
		}
		
		
		RandomGenerator randomGenerator = null;
		if(isJinWei()){
			randomGenerator = RandomGeneratorFactory.getInstance().getRandomGenerator(RandomGeneratorFactory.JIEWEI_RANDOM_GENERATOR);
		}else{
			randomGenerator = RandomGeneratorFactory.getInstance().getRandomGenerator(RandomGeneratorFactory.BUJIEWEI_RANDOM_GENERATOR);
		}
		
		//第二个数个位
		int k = randomGenerator.generateRandom(i,getGetWeiMax());
		
		//第二个数十位
		randomGenerator = RandomGeneratorFactory.getInstance().getRandomGenerator(RandomGeneratorFactory.BUJIEWEI_RANDOM_GENERATOR);
		int l = randomGenerator.generateRandom(j - 1,getShiWeiMax());
		
				
		int two = l * 10 + k;		
		
		Question question = new Question();
		
		if(isContinuousCalculation()){
			int three = 0;
			if(one - two != 0){
				three = RandomUtil.generateRandom(1, one - two);
			}
			
			question.setQuestion(one + "-" + two + "-" + three);
			question.setAnswer(String.valueOf(one - two - three));
			
		}else{
			question.setQuestion(one + "-" + two);
			question.setAnswer(String.valueOf(one - two));
		}
		
		return question;
	}
	
	public int getMin(){
		return 10;
	}
	
	public abstract int getMax();
	public abstract boolean isJinWei();
	public abstract boolean isContinuousCalculation();
	public abstract int getGetWeiMax();
	public abstract int getShiWeiMax();
	

}
